package com.example.mycarcalling.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.lib_cache.adapter.OrderNvAdapter;
import com.example.lib_cache.dao.OnOrderNvItemClickListener;
import com.example.lib_cache.model.Application;
import com.example.lib_cache.model.OrderNavItem;
import com.example.lib_cache.model.OrderWithNav;
import com.example.lib_cache.model.User;
import com.example.lib_cache.service.ApiService;
import com.example.lib_cache.viewmodel.OrderNavActivityViewModel;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityOrderNavBinding;

import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class OrderNavActivity extends AppCompatActivity {

    private final static String TAG = OrderNavActivity.class.getSimpleName();

    private Context context = null;
    private OrderNavActivityViewModel orderNavActivityViewModel = null;
    private ActivityOrderNavBinding activityOrderNavBinding = null;

    private OrderNvAdapter orderNvAdapter = null;
    private LinearLayoutManager llm = null;
    private ShowPersonDialog showPersonDialog = null;

    @Inject
    ApiService apiService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityOrderNavBinding = DataBindingUtil
                .setContentView(this, R.layout.activity_order_nav);
        context = this;

        initView();
        initData();
        initObserve();
        initListener();
        initRequest();

    }

    private void initView(){
        activityOrderNavBinding.setLifecycleOwner(this);
        llm = new LinearLayoutManager(context);
        activityOrderNavBinding.rvOrderNv.setLayoutManager(llm);
        showPersonDialog = new ShowPersonDialog(context);
        setTitle("历史订单");

    }

    private void initData(){
        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
        orderNavActivityViewModel = viewModelProvider.get(OrderNavActivityViewModel.class);
        orderNvAdapter = new OrderNvAdapter();
        activityOrderNavBinding.rvOrderNv.setAdapter(orderNvAdapter);
    }

    private void initListener(){
        //点击状态标签
        orderNvAdapter.setOnOrderNvItemClickListener(new OnOrderNvItemClickListener() {
            @Override
            public void showContextPerson(OrderNavItem orderNavItem) {
                Log.d(TAG,"显示司机");
                if(orderNavItem.getStatus().equals("已抵达 >")){
                    orderNavActivityViewModel
                            .requestPersonInfo(apiService,context,orderNavItem.getDriverId());
                }else {
                    Log.d(TAG,"继续订单");
                    if(orderNavItem.getStatus().equals("未出发 >")){
                        //结束当前界面，返回主页进入等待接单界面
                        setResult(10);
                        finish();
                    }
                    if(orderNavItem.getStatus().equals("开始中 >")){
                        //结束当前界面，返回主页请求订单状态
                        setResult(11);
                        finish();
                    }
                }

            }
        });
    }

    private void initObserve(){
        //观察无订单场景
        orderNavActivityViewModel.getResult().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    return;
                }
                Toast.makeText(context, "暂无订单", Toast.LENGTH_SHORT).show();
            }
        });

        //观察订单数据转换成功
        orderNavActivityViewModel.getOrderNavItems().observe(this, new Observer<List<OrderNavItem>>() {
            @Override
            public void onChanged(List<OrderNavItem> orderNavItems) {
                //设置适配器UI
                orderNvAdapter.updateData(orderNavItems);
            }
        });

        //观察订单数据获取成功
        orderNavActivityViewModel.getOrderWithNavLiveData().observe(this, new Observer<List<OrderWithNav>>() {
            @Override
            public void onChanged(List<OrderWithNav> orderWithNavs) {
                orderNavActivityViewModel.setOrderNavItems(
                        orderNavActivityViewModel.toOrderNavItem(orderWithNavs));
            }
        });

        orderNavActivityViewModel.getUserMutableLiveData().observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                orderNavActivityViewModel.requestApplication(apiService,context,user.getId());
            }
        });

        orderNavActivityViewModel.getApplicationMutableLiveData().observe(this, new Observer<Application>() {
            @Override
            public void onChanged(Application application) {
                //显示浮窗
                showPersonDialog.getTvLicensePlate().setText(application.getLicensePlate());
                showPersonDialog.getTvCarModel().setText(application.getCarModel());
                showPersonDialog.getTvCarColor().setText(application.getColor()+"色");
                showPersonDialog.getTvPersonNickName().setText(
                        orderNavActivityViewModel.getUserMutableLiveData().getValue()
                                .getNickname());
                showPersonDialog.show();

            }
        });
    }

    private void initRequest(){
        //发起获取订单请求
        orderNavActivityViewModel.requestHistoryOrders(apiService,context);
    }

}